package com.lanou.connotationjoke.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.adapter.ReviewMovieRecycleViewAdapter;
import com.lanou.connotationjoke.entity.AppUrl;
import com.lanou.connotationjoke.entity.ReviewDetialEty;
import com.lanou.connotationjoke.entity.ReviewEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class ReviewMovieFragment extends BaseFragment{
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private String URL = AppUrl.ReviewMovie;
    private ReviewMovieRecycleViewAdapter adapter;
    private ReviewEty reviewEty;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Toast.makeText(getActivity(), "刷新数据失败", Toast.LENGTH_SHORT).show();
            return false;
        }
    });


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_review_movie,null);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_recycleview);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycleview_review_movie);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        VolleySingleton.addRequest(URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                reviewEty = gson.fromJson(response, ReviewEty.class);
                List<ReviewDetialEty> datas = reviewEty.getT1348648650048();
                adapter = new ReviewMovieRecycleViewAdapter(getActivity(), datas);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                VolleySingleton.addRequest(URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        reviewEty = gson.fromJson(response, reviewEty.getClass());
                        List<ReviewDetialEty> datas = reviewEty.getT1348648650048();
                        adapter.refreshData(datas);
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        handler.sendEmptyMessage(0);
                    }
                });

            }

        });


    }

}
